
/*******************************************************************************
    STM32中有多达8个定时器，其中TIM1和TIM8是能够产生三对PWM互补输出的高级定时器，
常用于三相电机的驱动，它们的时钟由APB2(72Mhz)的输出产生。
   其它6个为普通定时器，时钟由APB1(36Mhz)的输出产生。
*******************************************************************************/
 //http://jwdz520.taobao.com  


#include "timer.h"

bool tim2_flag = false;

//通用定时(2~7)器挂在APB1总线上
//TIM_Period：自动重装值(u16)
//TIM_Prescaler：时钟预分频数(u16)
//定时时间计算  T=(TIM_Period+1)*(TIM_Prescaler+1)/TIMxCLK=(359+1)*(199+1)/36MHz=1ms 
void Timer2_Init(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;    
											
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;	  
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;	
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
   
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能
    TIM_TimeBaseStructure.TIM_Period = (1000-1);//累计 TIM_Period个频率后产生一个更新或者中断 
    TIM_TimeBaseStructure.TIM_Prescaler =(3600-1);	 //此值+1为时钟频率除数
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 	
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;/*TIM向上计数模式*/
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); 

    TIM_ClearFlag(TIM2, TIM_FLAG_Update);   
    TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); /*使能TIM2中断源*/
    TIM_Cmd(TIM2, ENABLE);    	             /*使能TIMx外设*/
}

//定时时间计算  T=(TIM_Period+1)*(TIM_Prescaler+1)/TIMxCLK=(359+1)*(199+1)/36MHz=1ms 
void Timer3_Init(void)
{
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        NVIC_InitTypeDef NVIC_InitStructure;   
                                                                                                        
        NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;       	// Enable the TIM3 Interrupt 
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 先占优先级 1 
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;    // 从优先级 1 
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;	      // IRQ通道被使能 
        NVIC_Init(&NVIC_InitStructure);

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能Tim3
	
	TIM_TimeBaseStructure.TIM_Period = (1000-1); //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	 
	TIM_TimeBaseStructure.TIM_Prescaler =(7200-1); //设置用来作为TIMx时钟频率除数的预分频值  
	TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
 
  	TIM_ClearFlag(TIM3, TIM_FLAG_Update);
        TIM_ITConfig(  //使能或者失能指定的TIM中断
	TIM3, //TIM3
	TIM_IT_Update  |  //TIM 中断源
	TIM_IT_Trigger,   //TIM 触发中断源 
	ENABLE  //使能
	);	
    TIM_Cmd(TIM3, ENABLE);  //使能TIMx外设							 
}

void TIM2_IRQHandler(void)   //TIM2中断 50ms
{
     static u16 tim2_count1,tim2_count2;
	if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 
        {
          TIM_ClearITPendingBit(TIM2, TIM_IT_Update  );  //清除TIMx的中断待处理位:TIM 中断源 
          tim2_count1++;tim2_count2++;
          if(tim2_count1==40) //50ms*40 =2000ms 
          {
            tim2_count1=0;
			tim2_flag = !tim2_flag;    // 用于轮流显示农历和节气 
          }	
		  if(tim2_count2==200) //50ms*200 =10000ms 
          {
           tim2_count2=0;	
	      //BLK_OFF;  
          }	
		  
       }
}

void TIM3_IRQHandler(void)   //TIM3中断
{
	
	if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 
	{
		TIM_ClearITPendingBit(TIM3, TIM_IT_Update  );  //清除TIMx的中断待处理位:TIM 中断源 

	      //LED2=!LED2;
	      // ADD YOUR CODE HERE
	}
}












